function gadget:GetInfo()
	return {
		name = "Levelup",
		desc = "general improvements on leveling up",
		author = "Sean Heron", 
		date = "2. January 2009",
		license = "GNU GPL v2, or later",
		layer = 28,  -- argh! the whole thing was screwed cause I was working on the same layer as experience.lua... doh
		enabled = true
	}
end

local debugon = false

-- the engines list is loaded in gadget:Initialise (from a shared config file)

local hpincreaseFactor = 1.5
local AttackSpeedChange = 0.9
local damageIncreaseFactor = 1.5

if (gadgetHandler:IsSyncedCode()) then

--SYNCED

function gadget:UnitCreated(u, ud, team)  -- team is as good as playerID ? not sharing are we (and if we were, we'd want the Engine lvl to be shared as well, huh)

	if engines[ud] then -- this is not true every time, is it ?
		local newdamage = UnitDefs[ud].health*hpincreaseFactor^(engineLevel[team]-1) -- rename to newHP below as well
		local proportionalHealth = Spring.GetUnitHealth(u)*hpincreaseFactor^(engineLevel[team]-1)
		Spring.SetUnitMaxHealth(u, newdamage)
		Spring.SetUnitHealth(u, proportionalHealth)
		Spring.Echo(newdamage)
		Spring.Echo(proportionalHealth)
		Spring.Echo("Engine ready to recieve some health!")
	end

end

function gadget:Initialize()  
	engineLevel = GG.engineLevel
	levelUp = GG.Levelup
	
	_, engines, _, _ = include("LuaRules/Configs/Engine_defs.lua")
end

function gadget:GameFrame(f)

	if f%30 < .1 then -- every second, ie every 30 frames?
		--Spring.Echo("come on !")
		local teams = Spring.GetTeamList()
		for teamID, _ in ipairs(teams) do  -- would ipairs be better (as elsewhere at times...)
			--Spring.Echo("is this ever called??")
			if levelUp[teamID] ~= 0 then  -- no error for the no Hero teams ?
				-- needs to be "is not 0"
				Spring.Echo("Engine leveled up to "..engineLevel[teamID])
				-- Spring.Echo(levelUp[teamID])
								
				local unitDef = Spring.GetUnitDefID(levelUp[teamID]) -- actually goes with Health atm
				
				-------------
				--Changing Health Points / ie MaxDamage
				--------------
				--why not call the current MaxDamage ? seems more sensible really, less likely to cock up, and doesn't require "hoch"
				local newdamage = UnitDefs[unitDef].health*hpincreaseFactor^(engineLevel[teamID]-1) -- this is checking against the original Maxdamage, I would hope - not versus our changed one...
			--	local currentHealth = 
				local proportionalHealth = Spring.GetUnitHealth(levelUp[teamID])*hpincreaseFactor^(engineLevel[teamID]-1)				
				Spring.Echo("Set Maxdamage to:")
				Spring.Echo(newdamage)
				Spring.SetUnitMaxHealth(levelUp[teamID], newdamage) 	
				Spring.SetUnitHealth(levelUp[teamID], proportionalHealth)

				-----------
				----Attack speed - ie reloadTime (Weapon -hmm both ?) - what about Engines with more or less than 2 weapons..
				
				-- -- this is playing up at the moment... reloadTime being made an integer..
				-- local qreloadTime = Spring.GetUnitWeaponState( levelUp[teamID], 1 , "reloadTime")	-- huh fixing up code after copying it around would be a good idea maybe...
				-- Spring.Echo("this being reduced by steps of one ??")
				-- Spring.Echo("It doesn't take only integers, so what's up ? ??")
				-- Spring.Echo(qreloadTime)	
				-- -- if need be I'll do it like this - don't see why though...
				-- local newReloadTime	= qreloadTime*AttackSpeedChange
				-- Spring.Echo("and the new time should be - but isn't :(")
				-- Spring.Echo(newReloadTime)
				-- local betterWeapon = {	
					-- reloadTime  = newReloadTime, -- should be continuously lowering - as the speed read out has been reduced by last LevelUp
				-- --	sprayAngle  = 800, --800	
				-- --	range       = 1200, --400		
				-- --	burst = 2, -- 4
				-- --	burstRate = .1, -- .3
				-- }		
				-- Spring.SetUnitWeaponState ( levelUp[teamID], 1, betterWeapon ) 
					


				----- reset levelUp value --
				levelUp[teamID] = 0 -- place higher ?
			end
		
		end
	end
end


function gadget:UnitPreDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)


	if engines[attackerDefID] then
		
		if debugon == true then
			Spring.Echo ("Damage before!")
			Spring.Echo (damage)
		end
		damage = damage * damageIncreaseFactor^(engineLevel[attackerTeam] - 1) -- this all I need to do ?
		if debugon == true then
			Spring.Echo ("And after...")
			Spring.Echo (damage)
			Spring.Echo ("Enginelevel?")
			Spring.Echo (engineLevel[unitTeam])
		end
	end
	return damage
	-- need to add the randomiser as well
end

else
--UNSYNCED


function gadget:Update()

end

end
